<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png" alt>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="toggleCollapse?'64px':'200px'">
        <div @click="toggleCollapse=!toggleCollapse" class="toggle">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="toggleCollapse"
          :collapse-transition="false"
          :default-active="defActive"
          router
        >
              <!-- index需要是字符串而不是数据，所以这里需要加个字符串 -->
          <el-submenu :index="item.id+''" :key="item.id" v-for="item in menusList">
            <template slot="title">
              <!-- 图标 -->
              <!-- 对象里的属性就是id，属性值就是相应的class -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 标题 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 子标签 -->
              <!-- index需要是字符串而不是数据，所以这里需要加个字符串 -->
              <el-menu-item :index="'/'+subItem.path " :key="subItem.id" v-for="subItem in item.children" @click="setDefActive('/'+subItem.path)">
                <!-- 子标签图标 -->
                <i class="el-icon-menu"></i>
                <!-- 子标签标题 -->
                <span>{{subItem.authName}}{{'/'+subItem.path}}</span>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menusList:[],
      iconsObj:{
        '125' : 'iconfont icon-users',
        "103": 'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      toggleCollapse:false,
      
      defActive:''
    }
  },
  created(){
    this.getMenuList(),

    this.defActive = window.sessionStorage.getItem('defActive')
  },
  methods: {
    logout() {
      // 退出时移除点击时保存的高亮事件
      window.sessionStorage.removeItem('defActive')
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    async getMenuList(){
      var {data:res} = await this.axios.get('menus')
      // console.log(res)
      if(res.meta.status != 200){
        return this.$message.error("服务异常")
      }
      // 把获取到的数据保留赋值给menusList
      this.menusList = res.data
      this.$message.success("亲，获取数据成功了呢")
    },
    //在本地保存点击高亮的事件
    setDefActive(path){
        window.sessionStorage.setItem('defActive',path)
        this.defActive = path
      }
  },


}
</script>

<style lang='less' scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #333747;
  display: flex;
  justify-content: space-between;
  padding: 10px 5px;
  div {
    display: flex;
    align-items: center;
    span {
      color: #fff;
      font-size: 25px;
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333700;
}
.el-main {
  background-color: #eaedff;
}
.iconfont{
  margin-right: 10px;
}
.el-menu{
  border-right: none;
}
.toggle{
  color:#fff;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  user-select: none;
  letter-spacing: 5px;
}
</style>
